module xdb_command_line

    use argparse, only: argparser
    implicit none

    private
    public :: cli_args

    type command_line
        character(256) :: working_dir = "."  !! working directory
        logical :: skip = .false.  !! skip asking for confirmation
    contains
        procedure :: parse
    end type command_line

    type(command_line) :: cli_args

contains

    !> Parse the command line arguments
    subroutine parse(self)
        class(command_line), intent(inout) :: self
        type(argparser) :: args
        character(*), parameter :: version_xdb(*) = &
            &[character(72) :: &
            "Version:     0.4.20230916, alpha (built on "//__DATE__//")", &
            "Program:     xdb", &
            "Description: A simple matetool for vscode + fortran", &
            "Homepage:    https://gitee.com/zoziha/xdb", &
            "Author:      ZUO Zhihua (zuo.zhihua@qq.com)", &
            "License:     MIT"]

        if (command_argument_count() == 0) return

        args = argparser("A simple matetool for vscode + fortran")

        call args%set_program_name("xdb")
        call args%add_help_option()
        call args%add_sc_option("-v", "--version", "show version info", &
                                show_version_info)
        call args%add_option_string("-C", "--directory", &
                                    "working directory", &
                                    ".")
        call args%add_option_logical("-s", "--skip", &
                                    "skip asking for confirmation")

        call args%parse()

        self%working_dir = args%get_option_string("-C")
        self%skip = args%get_option_logical("-s")

    contains

        subroutine show_version_info()

            print "(*(a/))", version_xdb

        end subroutine show_version_info

    end subroutine parse

end module xdb_command_line
